﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace resolutionClassTest
{
    class customResolutionSpriteBatch
    {
        private Microsoft.Xna.Framework.Graphics.SpriteBatch spriteBatch;
        private Microsoft.Xna.Framework.GraphicsDeviceManager graphics;
        private static int standardX = 1024;
        private static int standardY = 768;
        private bool isFullScreen = false;

        public customResolutionSpriteBatch(Microsoft.Xna.Framework.Graphics.SpriteBatch spriteBatch, Microsoft.Xna.Framework.GraphicsDeviceManager graphics)
        {
            this.spriteBatch = spriteBatch;
            this.graphics = graphics;
            this.graphics.PreferredBackBufferWidth = standardX;
            this.graphics.PreferredBackBufferHeight = standardY;

        }

        public customResolutionSpriteBatch() { }

        internal void Draw(Microsoft.Xna.Framework.Graphics.Texture2D texture1, Microsoft.Xna.Framework.Rectangle rect1)
        {

            if ((this.graphics.PreferredBackBufferHeight == 768) && (this.graphics.PreferredBackBufferWidth == 1024))
                spriteBatch.Draw(texture1, rect1, Color.White);
            else
            {
                int currentX = this.graphics.PreferredBackBufferWidth;
                int currentY = this.graphics.PreferredBackBufferHeight;

                double ratioX = (double)currentX / (double)standardX;
                double ratioY = (double)currentY / (double)standardY;

                if (isFullScreen == true)
                {
                    //this.graphics.PreferredBackBufferWidth = (int)(rect1.Width * ratioX);
                    //this.graphics.PreferredBackBufferHeight=(int)(rect1.Height * ratioY);
                }

                spriteBatch.Draw(texture1,
                    new Rectangle(
                        (int)(rect1.X * ratioX),
                        (int)(rect1.Y * ratioY),
                        (int)(rect1.Width * ratioX),
                        (int)(rect1.Height * ratioY)),
                        Color.White
                    );
            }
        }

        internal void ChangeResolutionTo(int p, int p_2)
        {
            this.graphics.PreferredBackBufferWidth = p;
            this.graphics.PreferredBackBufferHeight = p_2;

            this.graphics.ApplyChanges();
        }

        internal void ChangeResolutionToDefault()
        {
            this.graphics.PreferredBackBufferWidth = standardX;
            this.graphics.PreferredBackBufferHeight = standardY;
            this.graphics.ApplyChanges();

        }

        internal void setGraphicsDeviceManager(GraphicsDeviceManager graphics)
        {
            this.graphics = graphics;
        }

        internal void setSpriteBatch(Microsoft.Xna.Framework.Graphics.SpriteBatch spriteBatch)
        {
            this.spriteBatch = spriteBatch;
        }

        internal void SwitchToFullScreen()
        {
            this.graphics.IsFullScreen = true;
            this.isFullScreen = true;
            this.graphics.ApplyChanges();
        }

        internal void SwitchToWindowMode()
        {
            this.graphics.IsFullScreen = false;
            this.isFullScreen = false;
            this.graphics.ApplyChanges();
        }
    }
}
